import space  from '../namespaces'
var myspace = space.permission;


const mutations = {
  [myspace + '/initChekbox'](state, payload){
    state.loopData = payload.list
  },
  [myspace + '/initAdd'](state){
    state.control_group.name = ''
    state.control_group.depict = ''
  },
  //递归的给checkbox加上选择
  [myspace + '/checkme'](state, payload){
    updateCheck(state.loopData, payload.arg.id, payload.arg.check);

    //loopcheck(state.loopData,[payload.arg.id],payload.arg.check);//  全选功能

    loopcheckParent(state.loopData, payload.arg.id);//子类有选择则选择父类
    if (payload.arg.check == false) {
      loopcheck(state.loopData, [payload.arg.id], false);//父类不选择则子类都不选择
    }
  },
  [myspace + '/checkall'](state, payload){
    var checkIds = [];

    state.loopData.forEach(function (val, key) {
      if (val['parent_id'] == 0) {
        checkIds.push(val['id']);
      }
    });
    loopcheck(state.loopData, checkIds, payload.arg.check);
  },
  //提交事件
  [myspace + '/submitme'](state, payload){

  },
  [myspace + '/update_control_group'](state, payload){

    state.control_group.name = payload.name
    state.control_group.depict = payload.depict
  },
  [myspace + '/initOperate'](state, payload){
    initOperate(state, payload);
    //state.operates[payload.ctl]=payload.list
  }

}
/**
 *初始化
 */
function initOperate(state, payload) {

  let ctl = payload.rootState.current_ctl;
  let mybtns = payload.rootState.tabledata


  mybtns[ctl] = {
    EDIT: false,
    Detail: false,
    DELETE: false,
    FORBID: false,
    ADD: false,
    TOCK: false,//添加商品到仓库
    INFO: false,//编辑商品规格,
    CKINFO: false,//仓库规格
    SEARCH: false,//查询,
    EXPORT:false,//导出
  }

  payload.list.forEach(function (v, k) {


    switch (v) {
      case 'update':
        mybtns[ctl].EDIT = true
        break;
      case 'detail':
        mybtns[ctl].Detail = true
        break;

      case 'delete':
        mybtns[ctl].DELETE = true
        break;

      case 'forbid':
        mybtns[ctl].FORBID = true
        break;

      case 'insert':
        mybtns[ctl].ADD = true;
        break;

      case 'select':
        mybtns[ctl].SEARCH = true;
        break;
      case 'ckspinfo':
        mybtns[ctl].CKSPINFO = true
        break;
      case 'shopinfo':
        mybtns[ctl].INFO = true
        break;
      case 'export':
      case 'EXPORT':
	mybtns[ctl].EXPORT = true
	break;

      case 'specail'://所有
        Object.keys(mybtns[ctl]).forEach(function (key, val) {

          mybtns[ctl][key] = true
        });
        ;
        break;

      default:

        ;
        break;
    }

  });


}

function updateCheck(data, checkId, check) {


  data.forEach(function (v, k) {
    if (v['id'] == checkId) {
      v['check'] = check;
    }
  })

}

//全选  或 全不选

function loopcheck(data, checkIds, check) {

  checkIds.forEach(function (val, key) {
    data.forEach(function (v, k) {
      if (v.id == val) {
        v.check = check;

        if (v.children) {
          loopcheck(data, v.children, check);
        }

      }
    });
  });

}
/**
 *循环检查父类的子类是否选择了 有一个选择了就选择父类
 * 1.先获取父类
 * 2.父类的子类情况(都不选时，才不选)
 * 3.循环父类的父类
 */
function loopcheckParent(data, checkId) {

  //1.
  let parent = [];
  let now = [];
  data.forEach(function (v, k) {
    if (checkId == v['id']) {
      now = v;
    }
  });
  data.forEach(function (v, k) {
    if (now['parent_id'] == v['id']) {
      parent = v;
    }
  });


  //2.
  if (parent.length == 0) {
    return;
  }
  let parentCheck = false;
  parent['children'].forEach(function (vc, kc) {

    if (parentCheck != false) {
      return;
    }

    data.forEach(function (v, k) {

      if (vc == v['id']) {
        if (v['check'] == true) {//有一个选择的结束循环

          parentCheck = true;
          return;
        }
      }

    });

  });

  data.forEach(function (v, k) {
    if (parent['id'] == v['id']) {
      v['check'] = parentCheck;
    }
  });
  //3.
  if (parent['parent_id'] != 0) {
    loopcheckParent(data, parent['id']);
  }

}


export default mutations;
